class User < ActiveRecord::Base
  attr_protected :id, :active, :approved, :bonus
  acts_as_authentic
  has_and_belongs_to_many :roles
  has_many :orders
  
  
  validates :firstname, :length => {:within => 3..25}
  validates :lastname, :length => {:within => 3..25}
  validates :city, :length => {:within => 3..20}
  validates :address, :length => {:within => 3..40}
  validates :phone, :length => {:within => 7..12}
  validates :mobile, :length => {:within => 7..12}
  

  def set_new_password new_password
    self.code = nil
    self.password = new_password
    self.password_confirmation = new_password
    self.save
  end
  def get_roles
    self.roles.map do |item|
      item.name
    end
  end

  def set_roles roles_names
    self.role_ids = Role.find(:all, :conditions => ["name IN (?)", roles_names]).map do |item| item.id end
    self.save
  end
end